﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WhiteBoard
{
    public partial class ToolBar : Form
    {
        //定义移动相关变量

        bool IsMoving2 = false;
        Point pthisLastCoordinate = new Point(0, 0);
        Point pCursorOffset = new Point(0, 0);
        Point pCursorLastCoordinate = new Point(0, 0);

        public static BackGround bgForm;

        public ToolBar()
        {
            InitializeComponent();
          
        }

        private void ToolBar_Load(object sender, EventArgs e)
        {
            
        }

        private void btnCover_Click(object sender, EventArgs e)
        {
            ScreenCover scForm = new ScreenCover();
            scForm.ShowDialog();
        }

        private void btnSpotLight_Click(object sender, EventArgs e)
        {
            SpotLight slForm = new SpotLight();
            slForm.ShowDialog();
        }

        private void btnCamera_Click(object sender, EventArgs e)
        {
            ScreenCut1 screenCutForm = new ScreenCut1();
            screenCutForm.ShowDialog();
        }

        private void btnMagnifier_Click(object sender, EventArgs e)
        {
            Magnifier magnifyForm = new Magnifier();
            magnifyForm.Visible = false;
            magnifyForm.Show();
        }

    
        private void ToolBar_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                if (this.Cursor != Cursors.Hand)
                {
                    IsMoving2 = true;
                    pthisLastCoordinate.X = this.Left;
                    pthisLastCoordinate.Y = this.Top;
                    pCursorLastCoordinate.X = Cursor.Position.X;
                    pCursorLastCoordinate.Y = Cursor.Position.Y;
                }
            }
        }

        private void ToolBar_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                if (this.IsMoving2)
                {
                    Point pCursor = new Point(Cursor.Position.X, Cursor.Position.Y);
                    pCursorOffset.X = pCursor.X - pCursorLastCoordinate.X;
                    pCursorOffset.Y = pCursor.Y - pCursorLastCoordinate.Y;
                    this.Left = pthisLastCoordinate.X + pCursorOffset.X;
                    this.Top = pthisLastCoordinate.Y + pCursorOffset.Y;
                }
            }
        }

        private void ToolBar_MouseUp(object sender, MouseEventArgs e)
        {
            if (this.IsMoving2)
            {
                if (pCursorOffset.X == 0 && pCursorOffset.Y == 0)
                {
                    return;
                }
                if ((pthisLastCoordinate.X + pCursorOffset.X + this.Width) > 0)
                {
                    this.Left = pthisLastCoordinate.X + pCursorOffset.X;
                }
                else
                {
                    this.Left = 0;
                }
                if ((pthisLastCoordinate.Y + pCursorOffset.Y + this.Height) > 0)
                {
                    this.Top = pthisLastCoordinate.Y + pCursorOffset.Y;
                }
                else
                {
                    this.Top = 0;
                }

            }

            pCursorOffset.X = 0;
            pCursorOffset.Y = 0;
        }

        private void btnWhite_Click(object sender, EventArgs e)
        {
            bgForm = (BackGround)this.Owner;
            System.IO.FileStream fs;
            fs = new System.IO.FileStream(@"E:\程序\WhiteBoard\WhiteBoard\BoardLibrary\white.jpg", System.IO.FileMode.Open, System.IO.FileAccess.Read);
            bgForm.pictureBox0.Image = System.Drawing.Image.FromStream(fs);
            fs.Close();
            bgForm.CreateNewPage();
            bgForm.RefreshPage();
        }

        private void btnBlack_Click(object sender, EventArgs e)
        {
            bgForm = (BackGround)this.Owner;
            System.IO.FileStream fs;
            fs = new System.IO.FileStream(@"E:\程序\WhiteBoard\WhiteBoard\BoardLibrary\black.jpg", System.IO.FileMode.Open, System.IO.FileAccess.Read);
            bgForm.pictureBox0.Image = System.Drawing.Image.FromStream(fs);
            fs.Close();
            bgForm.CreateNewPage();
            bgForm.RefreshPage();
        }

        private void btnGreen_Click(object sender, EventArgs e)
        {
            bgForm = (BackGround)this.Owner;
            System.IO.FileStream fs;
            fs = new System.IO.FileStream(@"E:\程序\WhiteBoard\WhiteBoard\BoardLibrary\green.jpg", System.IO.FileMode.Open, System.IO.FileAccess.Read);
            bgForm.pictureBox0.Image = System.Drawing.Image.FromStream(fs);
            fs.Close();
            bgForm.CreateNewPage();
            bgForm.RefreshPage();
        }

        private void btnTool_Click(object sender, EventArgs e)
        {
            this.panel11.Visible = true;
            this.panel12.Visible = false;
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            this.panel12.Visible = true;
            this.panel11.Visible = false;
        }

        private void btnScreen_Click(object sender, EventArgs e)
        {
            Bitmap screenBmp = new Bitmap(Magnifier.fullScreen.Width,Magnifier.fullScreen.Height);
            Graphics gr = Graphics.FromImage(screenBmp);
            this.Opacity = 0;
            bgForm = (BackGround)this.Owner;
            bgForm.Opacity = 0;
            gr.CopyFromScreen(0,0,0,0,screenBmp.Size);
            bgForm.Opacity = 1;
            this.Opacity = 1;
            bgForm.pictureBox0.Image = screenBmp;
            gr.Dispose();
            bgForm.CreateNewPage();
            bgForm.RefreshPage();
        }

        private void btnResourse_Click(object sender, EventArgs e)
        {
            bgForm = (BackGround)this.Owner;
            OpenFileDialog openFile = new OpenFileDialog();
            openFile.Title = "选择已有背景";
            openFile.RestoreDirectory = true;
            openFile.InitialDirectory = @"E:\程序\WhiteBoard\WhiteBoard\BoardLibrary";
            openFile.Filter = "图形文件(*.bmp;*.jpg;*.jpeg;*.gif;*.wmf)|*.bmp;*.jpg;*.jpeg;*.gif;*.wmf";
            if (openFile.ShowDialog() == DialogResult.OK)
            {
                bgForm.pictureBox0.Image = Image.FromFile(openFile.FileName);
                bgForm.CreateNewPage();
                bgForm.RefreshPage();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            bgForm = (BackGround)this.Owner;
            bgForm.Close();
        }
    }
}
